//
//  OwnerHomeViewController.m
//  GoodWayClient
//
//  Created by newhuidong on 15/12/22.
//  Copyright © 2015年 HuiDong. All rights reserved.
//

#import "OwnerHomeViewController.h"
#import "AppDelegate.h"
#import "ExperienceVoucherManagerTableViewController.h"
#import "MyShopHomeViewController.h"
#import "CarOwnerLeftMenuViewController.h"
#import "PublicNoticeViewController.h"
#import "EmailViewController.h"
@interface OwnerHomeViewController ()
@property(nonatomic,strong)NSMutableArray * messageArray;
@property(nonatomic,assign)int mesgCount;

@end

@implementation OwnerHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.messageArray=[NSMutableArray arrayWithObjects:@"1",@"2",@"3", nil];
    self.mesgCount = 0;
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 20, 20);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"leftBarbtn"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
    self.title=@"首页";
    [self createBigScrollView];
    [self initButton];
    //return [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
    UIButton * rightBarButton =[UIButton buttonWithType:UIButtonTypeCustom];
    rightBarButton.frame = CGRectMake(self.view.frame.size.width/0.9, 20, 20, 20);
    [rightBarButton setBackgroundImage:[UIImage imageNamed:@"rightBarbtn"] forState:UIControlStateNormal];
    [rightBarButton addTarget:self action:@selector(openEmail) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBarButton];
    
    NSLog(@"你好啊！");
    NSLog(@"123456");
    

}
-(void)openEmail{
    
    EmailViewController*email = [[EmailViewController alloc]init];
    
    [self.navigationController pushViewController:email animated:YES];
    
    NSLog(@"打开邮件");
}
-(void)viewDidAppear:(BOOL)animated{
    
    [self createrollTitles];


    
}
//-(void)viewDidAppear:(BOOL)animated{
//    
//    [self mesgChange];
//    [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(mesgChange) userInfo:nil repeats:YES];
//    
//    
//}
//-(void)mesgChange{
//    
//    if (self.mesgCount<self.messageArray.count) {
//        self.textLabel.text = [self.messageArray objectAtIndex:self.mesgCount];
//        self.mesgCount++;
//    
//    }else{
//        self.textLabel.text =@"no message";
//    }
//    [self.textLabel sizeToFit];
//    
//    CGRect frame = self.textLabel.frame;
//    frame.origin.y =[UIScreen mainScreen].bounds.size.width/8-20;
//    self.textLabel.frame = frame;
//    [UIView beginAnimations:@"textLabel" context:NULL];
//    [UIView setAnimationDuration:2.0f];
//    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
//    [UIView setAnimationDelegate:self];
//    [UIView setAnimationRepeatAutoreverses:NO];
//    [UIView setAnimationRepeatCount:MAXFLOAT];
//    
//    frame = self.textLabel.frame;
//    frame.origin.y =-frame.size.width;
//    self.textLabel.frame = frame;
//    [UIView commitAnimations];
//    UITapGestureRecognizer*gesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
//    [self.textLabel addGestureRecognizer:gesture];
//    
//    
//}

-(void)viewWillAppear:(BOOL)animated{
    /*
     改变系统导航栏颜色
     */
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:27.0/255.0 green:144.0/255.0 blue:212.0/255.0 alpha:1];
    self.view.backgroundColor=[UIColor whiteColor];
    /*
     自定义标题颜色
     */
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:20],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    

    
}
-(void)initButton{
    
    self.leftButton.layer.cornerRadius=5;
    self.leftButton.layer.masksToBounds=NO;
    self.leftButton.layer.borderColor=[UIColor lightGrayColor].CGColor;
    self.leftButton.layer.borderWidth=0.3;
    self.leftButton.layer.shadowColor=[UIColor blackColor].CGColor;
    self.leftButton.layer.shadowOffset=CGSizeMake(0, 2);
    self.leftButton.layer.shadowOpacity=0.3;
    
    
    self.uprightButton.layer.cornerRadius=5;
    self.uprightButton.layer.masksToBounds=NO;
    self.uprightButton.layer.borderColor=[UIColor lightGrayColor].CGColor;
    self.uprightButton.layer.borderWidth=0.3;
    self.uprightButton.layer.shadowColor=[UIColor blackColor].CGColor;
    self.uprightButton.layer.shadowOffset=CGSizeMake(1, 2);
    self.uprightButton.layer.shadowOpacity=0.3;
    
    self.lowrightButton.layer.cornerRadius=5;
    self.lowrightButton.layer.masksToBounds=NO;
    self.lowrightButton.layer.borderColor=[UIColor lightGrayColor].CGColor;
    self.lowrightButton.layer.borderWidth=0.3;
    self.lowrightButton.layer.shadowColor=[UIColor blackColor].CGColor;
    self.lowrightButton.layer.shadowOffset=CGSizeMake(1, 2);
    self.lowrightButton.layer.shadowOpacity=0.3;
    
    self.bottomButton.layer.cornerRadius=5;
    self.bottomButton.layer.masksToBounds=NO;
    self.bottomButton.layer.borderColor=[UIColor lightGrayColor].CGColor;
    self.bottomButton.layer.borderWidth=1;
    
    self.backgroundView.backgroundColor=[UIColor whiteColor];
    self.backgroundView.layer.borderColor=[UIColor lightGrayColor].CGColor;
    self.backgroundView.layer.borderWidth=0.3;

    self.backgroundView.layer.shadowColor=[UIColor blackColor].CGColor;
    self.backgroundView.layer.shadowOffset=CGSizeMake(0, 2);
    self.backgroundView.layer.shadowOpacity=0.3;

    
}
-(void)createrollTitles{
    
    self.textLabel.text=@"路之好蓝天主打产品除碳王,除碳服务";
    
    CGRect frame = self.textLabel.frame;
    
    frame.origin.y =-40;
    self.textLabel.frame =frame;
    [UIView beginAnimations:@"rollTitles" context:NULL];
    [UIView setAnimationDuration:3.0f];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationRepeatCount:MAXFLOAT];
    frame =self.textLabel.frame;
    frame.origin.y = 10;
    self.textLabel.frame=frame;
    [UIView commitAnimations];
    
    self.backgroundView.layer.shadowColor=[UIColor blackColor].CGColor;
    
    self.backgroundView.layer.shadowOffset=CGSizeMake(0, 2);
    self.backgroundView.layer.shadowOpacity=0.3;
    self.textLabel.tag=1;
    UITapGestureRecognizer * tapGesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
    [_textLabel addGestureRecognizer:tapGesture];
    _textLabel.userInteractionEnabled=YES;
    
   
    
}
-(void)click:(UITapGestureRecognizer*)gesture{
    
    PublicNoticeViewController* public =[[PublicNoticeViewController alloc]init];
    [self.navigationController pushViewController:public animated:YES];
    
//    [self presentViewController:public animated:YES completion:nil];
//    NSLog(@"触发点击事件");
    
}
-(void)createBigScrollView{
    
    
    CGFloat imageW = self.view.frame.size.width;
    CGFloat imageH = self.bigScrollView.frame.size.height;
    
    CGFloat imageY =0;
    NSInteger totalCount = 3;
    
    for (int i =0; i<10; i++) {
        UIImageView * imageView = [[UIImageView alloc]init];
        CGFloat imageX = i* imageW;
        imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
        
        NSString * name = [NSString stringWithFormat:@"%d.jpg",i+7];
        imageView.image=[UIImage imageNamed:name];
        self.bigScrollView.showsHorizontalScrollIndicator=NO;
        [self.bigScrollView addSubview:imageView];
    }
    
    CGFloat contentW = totalCount * imageW;
    
    self.bigScrollView.contentSize = CGSizeMake(contentW, 0);
    
    self.bigScrollView.pagingEnabled=YES;
    
    self.bigScrollView.delegate=self;
    [self addTimer];
    
    self.pageControll.numberOfPages =3;
    self.pageControll.currentPage=0;
    self.pageControll.currentPageIndicatorTintColor=[UIColor colorWithRed:26/255.0f green:142/255.0f blue:207/255.0f alpha:1];
    [self.view addSubview:self.pageControll];
    
    
    
}
-(void)nextImage{
    
    //self.pageControll.currentPage=0;
    //[self.view addSubview:self.pageControll];
    int page = (int)self.pageControll.currentPage;
    if (page==2) {
        
        page=0;
        
    }else{
        
        page++;
        
    }
    
    //滚动scrollView
    CGFloat x = page * self.bigScrollView.frame.size.width;
    self.bigScrollView.contentOffset = CGPointMake(x, 0);
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // NSLog(@"滚动中");
    //计算页码
    // 页码=（contentoffset.x +scrollView 一半宽度）/scrollView宽度
    CGFloat scrollviewW = scrollView.frame.size.width;
    CGFloat x =scrollView.contentOffset.x;
    int page =(x + scrollviewW/2)/scrollviewW;
    self.pageControll.currentPage =page;
    
    
}
//开始拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //关闭定时器
    [self removeTimer];
    
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //开启定时器
    [self addTimer];
}
//开启定时器

-(void)addTimer{
    
    self.timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    
    
}
//关闭定时器
-(void)removeTimer{
    
    
    [self.timer invalidate];
    
    
}
- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}


@end
